<template>
  <Form ref="form" :model="form" :rules="rule" :label-width="100">
    <FormItem label="所属药店" prop="shopId">
      <Select ref="selectShop" v-model="form.shopId"
      filterable
      remote
      :remote-method="remoteQueryShop"
      :loading="loading"
      clearable>
        <Option v-for="(item,index) in shopList" :value="item.id" :label="item.name" :key="index">
            <span>{{item.name}}</span>
            <span style="float:right;">{{item.province}}{{item.city}}{{item.district}}</span>
        </Option>
       </Select>
    </FormItem>
    <FormItem label="姓名" prop="realName">
      <Input v-model="form.realName" placeholder=""></Input>
    </FormItem>
    <FormItem label="手机号" prop="mobilePhone">
      <Input v-model="form.mobilePhone" placeholder=""></Input>
    </FormItem>
    <FormItem label="是否锁定" prop="isLocked">
      <RadioGroup v-model="form.isLocked">
          <Radio :label="1">是</Radio>
          <Radio :label="0">否</Radio>
      </RadioGroup>
    </FormItem>
  </Form>
  </template>
<script>
import { save, update, get } from '@/api/wos/wos.user.service.js'
import { listByPage as listShopByPage } from '@/api/wzyy/wzyy.shop.service.js'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      loading: false,
      shopList: [],
      form: {
        id: 0,
        shopId: '',
        realName: '',
        mobilePhone: '',
        isLocked: 0
      },
      rule: {
        shopId: [
          { required: true, message: '店铺ID不能为空', trigger: 'blur' }
        ],
        realName: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        mobilePhone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    if (this.isEdit) {
      get({
        id: this.params.id
      }).then(res => {
        this.form = this.$util.copy(res.data, this.form)
      })
    } else {
      this.remoteQueryShop('')
    }
  },
  methods: {
    remoteQueryShop (k) {
      var index = this.shopList.findIndex(item => {
        return item.name === k
      })
      if (index !== -1) {
        return
      }
      listShopByPage({
        searchMap: {
          keywords: k,
          shopId: this.form.shopId
        }
      }).then(res => {
        this.shopList = res.data.rows
      })
    },
    submit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.isEdit) {
              update(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            } else {
              delete this.form.id
              save(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            }
          } else {
            reject(new Error('error'))
          }
        })
      })
    }
  }
}
</script>
